package com.fourth.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fourth.dao.BottomArticleDao;
import com.ljx.entity.BottomArticle;
import com.ljx.entity.Link;
import com.ljx.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author：lmy
 * @Date：2023/3/1 16:54
 */
@RestController
@RequestMapping("home/api/bottomArticle")
public class ApiBottomArticleController {

    @Autowired(required = false)
    private BottomArticleDao bottomArticleDao;

    @PostMapping("getBottomArticle")
    public Result getBottomArticle(@RequestParam Integer currentPage, @RequestParam Integer pageSize,
                             @RequestBody BottomArticle bottomArticle){

        Page<BottomArticle> page = new Page<>(currentPage,pageSize);
        QueryWrapper<BottomArticle> wrapper = new QueryWrapper<>();
        if (bottomArticle.getName() != null){
            wrapper.like("name",bottomArticle.getName());
        }
        if (bottomArticle.getIsDisable() != null){
            wrapper.like("is_disable",bottomArticle.getIsDisable());
        }

        Page<BottomArticle> bottomArticlePage = bottomArticleDao.selectPage(page, wrapper);
        return new Result(2000,"查询成功",bottomArticlePage);

    }

    @GetMapping("changeStatus")
    public Result changeStatus(@RequestParam String id,@RequestParam Integer isDisable){
        BottomArticle bottomArticle = new BottomArticle();
        bottomArticle.setId(id);
        bottomArticle.setIsDisable(isDisable);
        int row = bottomArticleDao.updateById(bottomArticle);
        if (row == 1){
            return new Result(2000,"修改状态成功");
        }
        return  new Result(5000,"修改状态失败");
    }

    @GetMapping("deleteBottomArticle")
    public Result deleteBottomArticle(@RequestParam String id){
        int row = bottomArticleDao.deleteById(id);
        if (row == 1){
            return new Result(2000,"删除成功");
        }
        return  new Result(5000,"删除失败");
    }

    @PostMapping("addBottomArticle")
    public Result addBottomArticle(@RequestBody BottomArticle bottomArticle){
        int row = bottomArticleDao.insert(bottomArticle);
        if (row == 1){
            return new Result(2000,"添加成功");
        }
        return  new Result(5000,"添加失败");
    }

    @PostMapping("editBottomArticle")
    public Result editBottomArticle(@RequestBody BottomArticle bottomArticle){
        int row = bottomArticleDao.updateById(bottomArticle);
        if (row == 1){
            return new Result(2000,"修改成功");
        }
        return  new Result(5000,"修改失败");
    }



}
